package com.itheima.web.controller.ums;

import com.itheima.result.ResultData;
import com.itheima.service.ums.UmsResourceService;
import com.itheima.vo.ums.UmsResource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("resource")
public class UmsResourceController {
    @DubboReference
    private UmsResourceService umsResourceService;

    @PostMapping
    public ResultData create(@RequestBody UmsResource umsResource) {
        umsResourceService.save(umsResource);
        return ResultData.ok("操作成功", "");
    }

    @PostMapping("delete/{id}")
    public ResultData delete(@PathVariable Integer id) {
        umsResourceService.removeById(id);
        return ResultData.ok("操作成功", "");
    }

    @GetMapping("list")
    public ResultData list(Integer categoryId, String nameKeyword, String urlKeyword, Integer pageNum, Integer pageSize) {
        return umsResourceService.pageList(categoryId, nameKeyword, urlKeyword, pageNum, pageSize);
    }

    @GetMapping("listAll")
    public ResultData listAll() {
        return ResultData.ok("操作成功", umsResourceService.list());
    }

    @PostMapping("update/{id}")
    public ResultData update(@PathVariable Integer id, @RequestBody UmsResource umsResource) {
        umsResource.setId(id);
        umsResourceService.updateById(umsResource);
        return ResultData.ok("操作成功", "");
    }

    @GetMapping("{id}")
    public ResultData getById(@PathVariable Integer id) {
        return ResultData.ok("操作成功", umsResourceService.getById(id));
    }
}
